package com.yj.springbootmybatis.rest;

import io.swagger.annotations.Api;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@Api(tags = "测试递归搜索")
public class TestController {


    private static final List<Total> list = new ArrayList<>();

    static {


        Total total1 = new Total();
        Total total2 = new Total();

        List<Total> last_children1 = new ArrayList<>();
        Total last1 = new Total().setTitle("MAHKD006033501").setAccount(6090001);
        Total last2 = new Total().setTitle("MAHKD006033502").setAccount(6090002);
        Total last3 = new Total().setTitle("MAHKD006033503").setAccount(6090003);
        last_children1.add(last1);
        last_children1.add(last2);
        last_children1.add(last3);

        List<Total> last_children2 = new ArrayList<>();
        Total last4 = new Total().setTitle("MAHKD006033501").setAccount(2090001);
        Total last5 = new Total().setTitle("MAHKD006033502").setAccount(2090002);
        Total last6 = new Total().setTitle("MAHKD006033503").setAccount(2090003);
        Total last7 = new Total().setTitle("MAHKD006033503").setAccount(2090004);
        last_children2.add(last4);
        last_children2.add(last5);
        last_children2.add(last6);
        last_children2.add(last7);


        Total total1_children1 = new Total().setTitle("10900").setAccount(6090001).setChildren(last_children1);
        Total total1_children2 = new Total().setTitle("20900").setAccount(2090001).setChildren(last_children2);

        ArrayList<Total> totals1 = new ArrayList<>();
        ArrayList<Total> totals2 = new ArrayList<>();
        totals1.add(total1_children1);
        totals2.add(total1_children2);


        total1.setAccount(1000).setTitle("root1").setChildren(totals1);
        total2.setAccount(2000).setTitle("root2").setChildren(totals2);

        list.add(total1);
        list.add(total2);


    }

    public static void main(String[] args) {
        System.out.println(list);
        List list1 = get("10900");
        System.out.println(list1);
    }

    @GetMapping("/getAll")
    public List getAll() {
        return list;
    }

//    @GetMapping("/get/{title}")
    public static List get(@PathVariable String title) {

        List<Total> newList = new ArrayList<>(list);
        List list1 = findTitle(newList, title);

        return list1;

    }


    public static List findTitle(List<Total> newlist, String title) {

        if (CollectionUtils.isEmpty(newlist)) {
            return null;
        }
        List<Total> newTotalLists = new ArrayList<>();
        for (Total total : newlist) {
            if (!Objects.isNull(title) && total.getTitle().equals(title)) {
                newTotalLists.add(total);
            } else {
                List<Total> temp1 = findTitle(total.getChildren(), title);
                List<Total> temp2 = new ArrayList<>();
                if (!CollectionUtils.isEmpty(temp1) && temp1.size() > 0) {
                    temp2.addAll(temp1);
                    total.setChildren(temp2);
                    newTotalLists.add(total);
                }
            }
        }

        return newTotalLists;
    }


}

@Data
@Accessors(chain = true)
class Total implements Serializable {

    private String title;
    private Integer account;
    private List<Total> children;
}